﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using GBFinance.Entity;
using HiLand.Framework.FoundationLayer;
using HiLand.Utility.DataBase;

namespace GBFinance.DAL
{
    public class SecuredPropertyDAL : BaseSqlDAL<SecuredPropertyEntity>
    {
        #region 基本信息
        protected override string TableName
        {
            get { return "LoanSecuredProperty"; }
        }

        protected override string[] KeyNames
        {
            get { return new string[] { "ItemGuid" }; }
        }

        /// <summary>
        /// Guid主键名称
        /// </summary>
        protected override string GuidKeyName
        {
            get { return "ItemGuid" ; }
        }

        protected override string PagingSPName
        {
            get { return "usp_Loan_SecuredProperty_SelectPaging"; }
        }
        #endregion
        public override bool Create(SecuredPropertyEntity entity)
        {
            string commandText = @"Insert Into [LoanSecuredProperty] (
			        [ItemGuid],
			        [ItemName],
			        [ItemNo],
			        [ItemKind],
			        [ItemType],
			        [ItemDetail],
			        [ItemValue],
			        [ItemOwerName],
			        [UserGuid],
			        [PropertyNames],
			        [PropertyValues]
                ) 
                Values (
			        @ItemGuid,
			        @ItemName,
			        @ItemNo,
			        @ItemKind,
			        @ItemType,
			        @ItemDetail,
			        @ItemValue,
			        @ItemOwerName,
			        @UserGuid,
			        @PropertyNames,
			        @PropertyValues
                )";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.Instance.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }

        public override bool Update(SecuredPropertyEntity entity)
        {
            string commandText = @"Update [LoanSecuredProperty] Set   
					[ItemGuid] = @ItemGuid,
					[ItemName] = @ItemName,
					[ItemNo] = @ItemNo,
					[ItemKind] = @ItemKind,
					[ItemType] = @ItemType,
					[ItemDetail] = @ItemDetail,
					[ItemValue] = @ItemValue,
					[ItemOwerName] = @ItemOwerName,
					[UserGuid] = @UserGuid,
					[PropertyNames] = @PropertyNames,
					[PropertyValues] = @PropertyValues
             Where [ItemGuid] = @ItemGuid";

            SqlParameter[] sqlParas = PrepareParasAll(entity);

            bool isSuccessful = SqlHelperEx.Instance.ExecuteSingleRowNonQuery(commandText, sqlParas);
            return isSuccessful;
        }
        #region 辅助方法
        protected override void InnerPrepareParasAll(SecuredPropertyEntity entity, ref List<SqlParameter> paraList)
        {
            List<SqlParameter> list = new List<SqlParameter>() 
            {
			    new SqlParameter("@ItemGuid",entity.ItemGuid== Guid.Empty?Guid.NewGuid():entity.ItemGuid),
			    new SqlParameter("@ItemName",entity.ItemName),
			    new SqlParameter("@ItemNo",entity.ItemNo),
			    new SqlParameter("@ItemKind",entity.ItemKind),
			    new SqlParameter("@ItemType",entity.ItemType),
			    new SqlParameter("@ItemDetail",entity.ItemDetail??string.Empty),
			    new SqlParameter("@ItemValue",entity.ItemValue),
			    new SqlParameter("@ItemOwerName",entity.ItemOwerName??string.Empty),
			    new SqlParameter("@UserGuid",entity.UserGuid)
            };

            paraList.AddRange(list);
        }

        protected override SecuredPropertyEntity Load(IDataReader reader)
        {
            SecuredPropertyEntity entity = new SecuredPropertyEntity();
            if (reader != null && reader.IsClosed == false)
            {
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemID"))
                {
                    entity.ItemID = reader.GetInt32(reader.GetOrdinal("ItemID"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemGuid"))
                {
                    entity.ItemGuid = reader.GetGuid(reader.GetOrdinal("ItemGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemName"))
                {
                    entity.ItemName = reader.GetString(reader.GetOrdinal("ItemName"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemNo"))
                {
                    entity.ItemNo = reader.GetInt32(reader.GetOrdinal("ItemNo"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemKind"))
                {
                    entity.ItemKind = reader.GetInt32(reader.GetOrdinal("ItemKind"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemType"))
                {
                    entity.ItemType = reader.GetInt32(reader.GetOrdinal("ItemType"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemDetail"))
                {
                    entity.ItemDetail = reader.GetString(reader.GetOrdinal("ItemDetail"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemValue"))
                {
                    entity.ItemValue = reader.GetDecimal(reader.GetOrdinal("ItemValue"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "ItemOwerName"))
                {
                    entity.ItemOwerName = reader.GetString(reader.GetOrdinal("ItemOwerName"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "UserGuid"))
                {
                    entity.UserGuid = reader.GetGuid(reader.GetOrdinal("UserGuid"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyNames"))
                {
                    entity.PropertyNames = reader.GetString(reader.GetOrdinal("PropertyNames"));
                }
                if (DataReaderHelper.IsExistFieldAndNotNull(reader, "PropertyValues"))
                {
                    entity.PropertyValues = reader.GetString(reader.GetOrdinal("PropertyValues"));
                }
            }
            return entity;
        }
        #endregion
    }
}
